package com.asia5b.a5mall.goods.dialog;

import android.text.TextUtils;

import com.asia5b.a5mall.middleware.model.goods.Goods2SkuModel;
import com.asia5b.a5mall.middleware.model.goods.Goods2SpecModel;

import java.io.Serializable;
import java.util.ArrayList;

import timber.log.Timber;

/**
 * Created by zhangyuncai on 2020/1/20.
 * todo,核心思想就是将每列的spec_value_id组合出所有情况并判断有无库存
 */
public class GoodsSpecHelper implements Serializable {
    //todo sku集合
    private final ArrayList<Goods2SkuModel> skuList;


    //todo 两个adapter中position二维集合(RecyclerView嵌套RecyclerView),外面RecyclerView的position称为yShaft,y轴,里面的称为xShaft,x轴,也就是y轴上有多列x轴集合
    private final ArrayList<ArrayList<Goods2SpecModel>> yShaftList;
    private final ArrayList<String> inventoryList;

    //todo 一个组合(每个x轴拿出一个index的组合)
    public ArrayList<Integer> mComb = new ArrayList<>();
    //todo 组合集合
    public ArrayList<ArrayList<Integer>> mCombList = new ArrayList<>();

    //当前选中的组合
    public ArrayList<Integer> mCurrentComb = null;
    public Goods2SkuModel currentGoods2SkuModel = null;

    /**
     * 构造方法
     * @param skuList 属性组合 譬如 红色-30码-女装鞋(用字段spec_list表示)就是一个sku
     * @param objectSecList
     * @param inventoryList
     */
    public GoodsSpecHelper(ArrayList<Goods2SkuModel> skuList, ArrayList<ArrayList<Goods2SpecModel>> objectSecList, ArrayList<String> inventoryList) {
        this.skuList = skuList;
        this.yShaftList = objectSecList;
        this.inventoryList = inventoryList;
//        for (int yShaft= 0; yShaft < yShaftList.size(); yShaft++) {
//            ArrayList<Goods2SpecModel> goods2SpecModels = yShaftList.get(yShaft);
//            for (int xShaft = 0; xShaft < goods2SpecModels.size(); xShaft++) {
//                int spec_value_id = goods2SpecModels.get(yShaft).getSpec_value_id();
//            }
//        }
        if (yShaftList != null) {
            yShaftFor(0);
        }
        //是否有初始选中组合
        for (ArrayList<Integer> integers : mCombList) {
            //给一个初始选中
            if (checkInventory(integers)) {
                findPrice(integers);
                break;
            }
        }
    }

    public void findPrice(ArrayList<Integer> integers) {
        mCurrentComb = integers;
        for (int yShaft = 0; yShaft < integers.size(); yShaft++) {
            Integer spec_value_id = integers.get(yShaft);
            ArrayList<Goods2SpecModel> goods2SpecModels = yShaftList.get(yShaft);
            for (Goods2SpecModel goods2SpecModel : goods2SpecModels) {
                if (goods2SpecModel.getSpec_value_id() == spec_value_id) {
                    goods2SpecModel.setBgState(1);
                } else {
                    //组装新的组合再设置背景
                    ArrayList<Integer> tempList = new ArrayList<>();
                    for (int i = 0; i < integers.size(); i++) {
                        if (i == yShaft) {
                            tempList.add(goods2SpecModel.getSpec_value_id());
                        } else {
                            tempList.add(mCurrentComb.get(i));
                        }
                    }

                    goods2SpecModel.setBgState(checkInventory(tempList) ? 0 : 2);
                }
            }
        }
        if (mCurrentComb != null) {
            for (Goods2SkuModel goods2SkuModel : skuList) {
                long a = 0;
                for (Integer integer : goods2SkuModel.getSpec_value_id_list()) {
                    a += integer;
                }
                long b = 0;
                for (Integer integer : mCurrentComb) {
                    b += integer;
                }
                if (a == b) {
                    currentGoods2SkuModel = goods2SkuModel;
                    Timber.d("--->>>>>>>>---当前库存:" + currentGoods2SkuModel.getEnable_quantity());
                } else {
                    if (TextUtils.equals(goods2SkuModel.getSpec_value_id_list_text(), mCurrentComb.toString())) {
                        currentGoods2SkuModel = goods2SkuModel;
                        Timber.d("--->>>>>>>>---当前库存:" + currentGoods2SkuModel.getEnable_quantity());
                    }
                }
            }
        }
    }

    /**
     * 是否有库存
     *
     * @param comb 一个组合(每个x轴拿出一个index的组合)
     * @return
     */
    public boolean checkInventory(ArrayList<Integer> comb) {
        a:
        for (String s : inventoryList) {
            boolean has = true;
            for (Integer integer : comb) {
                if (!(s.contains("" + integer))) {
                    continue a;
                }
            }
            if (has) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param yShaft        第几层
     * @param spec_value_id
     * @return
     */
    public boolean checkInventory(int yShaft, int spec_value_id) {
        ArrayList<Integer> integers = new ArrayList<>();
        if (mCurrentComb != null) {
            for (int i = 0; i < mCurrentComb.size(); i++) {
                if (i == yShaft) {
                    integers.add(spec_value_id);
                } else {
                    integers.add(mCurrentComb.get(i));
                }
            }
        }
        if (checkInventory(integers)) {
            findPrice(integers);
            return true;
        }
        return false;
    }


    /**
     * 对Y轴循环
     *
     * @param yShaft
     */
    void yShaftFor(int yShaft) {
        //防止死循环
        if (yShaft >= yShaftList.size()) {
            return;
        }
        ArrayList<Goods2SpecModel> specList = yShaftList.get(yShaft);
        for (Goods2SpecModel goods2SpecModel : specList) {
            //方便转换
            ArrayList<Integer> combTemp = new ArrayList<>();
            //todo 保证comb.size==yShaft(每层拿出一个index之前,保证组合中的数量等于层数,例如第0层,只能有0个index)
            for (int j = 0; j < yShaft; j++) {
                combTemp.add(mComb.get(j));
            }
            mComb = combTemp;
            append(yShaft, goods2SpecModel.getSpec_value_id());
        }

    }

    /**
     * 将xShaft添加到组合中,组合满足要求的添加到组合集合中
     *
     * @param yShaft      y轴的角标
     * @param specValueId x轴的角标
     */
    void append(int yShaft, int specValueId) {
        //防止死循环
        if (yShaft >= yShaftList.size()) {
        } else {
            mComb.add(specValueId);
            if (mComb.size() == yShaftList.size()) {
                mCombList.add(mComb);
            }
            yShaftFor(yShaft + 1);
        }
    }
}
